package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.File;
import com.ruoyi.system.domain.dto.FileUploadDTO;
import com.ruoyi.system.service.ISysFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 文件上传Controller
 *
 * @author ruoyi
 * @date 2024-07-01
 */
@RestController
@RequestMapping("/system/file")
public class SysFileController extends BaseController {
    @Autowired
    private ISysFileService sysFileService;

    /**
     * 查询文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:list')")
    @GetMapping("/list")
    public TableDataInfo list(File sysFile) {
        startPage();
        List<File> list = sysFileService.selectSysFileList(sysFile);
        return getDataTable(list);
    }

    /**
     * 导出文件上传列表
     */
    @PreAuthorize("@ss.hasPermi('system:file:export')")
    @Log(title = "文件上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, File sysFile) {
        List<File> list = sysFileService.selectSysFileList(sysFile);
        ExcelUtil<File> util = new ExcelUtil<File>(File.class);
        util.exportExcel(response, list, "文件上传数据");
    }

    /**
     * 获取文件上传详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:file:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysFileService.selectSysFileById(id));
    }

    /**
     * 新增文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:file:add')")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody File sysFile) {
        return toAjax(sysFileService.insertSysFile(sysFile));
    }

    /**
     * 修改文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:file:edit')")
    @Log(title = "文件上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody File sysFile) {
        return toAjax(sysFileService.updateSysFile(sysFile));
    }

    /**
     * 删除文件上传
     */
    @PreAuthorize("@ss.hasPermi('system:file:remove')")
    @Log(title = "文件上传", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(sysFileService.deleteSysFileByIds(id));
    }

    /**
     * 预上传
     *
     * @param params
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(@Valid @RequestBody FileUploadDTO params) {
        return AjaxResult.success(sysFileService.upload(params));
    }

    /**
     * 获取每个分片的预签名上传地址
     *
     * @param fileMd5    文件唯一标识
     * @param partNumber 分片
     * @return 文件预签名上传地址
     */
    @GetMapping("/{fileMd5}/{partNumber}")
    public AjaxResult getPreSignUploadUrl(@PathVariable("fileMd5") String fileMd5, @PathVariable("partNumber") Integer partNumber) {
        return AjaxResult.success().put("data", sysFileService.getPreSignUploadUrl(fileMd5, partNumber));
    }

    /**
     * 合并分片
     *
     * @param fileMd5
     * @return
     */
    @PostMapping("/merge/{fileMd5}")
    public AjaxResult merge(@PathVariable("fileMd5") String fileMd5) {
        sysFileService.merge(fileMd5);
        return success();
    }

    @PostMapping("/download/{fileMd5}")
    public void download(@PathVariable("fileMd5") String fileMd5, HttpServletResponse response) {
        sysFileService.download(fileMd5, response);
    }
}
